<?php
	class RbacAction extends CommonAction{
        //网站音乐调试
        public function miusic(){
            $this->display();
            //$this->display('add_miusic');
        }
        //添加音乐
        public function run_add_miusic(){
            $url=C('WEB_MIUSIC');
            delete_file($url);
            if (!empty($_FILES['miusic_file']['name'])) {

                //如果有文件上传 上传附件
                $this->uploadify_miusic();}

            if(F('miusic',$_POST,CONF_PATH)){
                $this->success('修改成功',U(GROUP_NAME.'/Rbac/miusic'));
            }else{
                $this->error('修改失败');
            }
        }
		public function index(){
			$this->user = D('UserRelation')->field('password', true)->relation(true)->where(array('username'=>array('neq',C('RBAC_SUPERADMIN'))))->select(); //大小写
			$this->display();
		}
		//角色列表
		public function role(){
			$this->role = M('role')->select();
			$this->display();
		}
		//节点列表
		public function node(){
			$field = array('id','name','title','pid');
			$node = M('node')->field($field)->order('sort')->select();
			$this->node = node_merge($node);
			$this->display();
		}
		//添加用户
		public function addUser(){
			$this->role = M('role')->select();
			$this->display();
		}
		//添加用户处理
		public function addUserHandle(){
            $user = M('user')->where(array('username' => array('eq', I('username'))))->select();
            if ($user) {
                $this->error('账户已存在');
            }
            if(I('password1')!=I('password')){
                $this->error(' so sorry password verify faild!!');
            }
			$user = array(
				'username' => $_POST['username'],
				'password' => md5($_POST['password']),
				'logintime' => time(),
				'loginip' => get_client_ip(),
                'realname'=>$_POST['realname']
			);
			if($uid = M('user')->add($user)){
				foreach($_POST['role_id'] as $v){
					$role[] = array(
						'role_id' => $v,
						'user_id' => $uid,
					);
				}
				M('role_user')->addAll($role);
				$this->success('添加成功', 'index');
			} else {
				$this->error('添加失败');
			}
		}
		//添加角色
		public function addRole(){
			//echo '添加角色';
			$this->display();
		}
		//角色添加处理
		public function addRoleHandle(){
			if(M('role')->add($_POST)){
				$this->success('添加成功', U(GROUP_NAME.'/Rbac/role'));
			} else {
				$this->error('添加失败');
			}
		}
		//添加节点
		public function addNode(){
			$this->pid = I('pid', 0, 'intval');
			$this->level = I('level', 1, 'intval');
			switch($this->level){
				case 1:
					$this->type = "应用";
					break;
				case 2:
					$this->type = "控制器";
					break;
				case 3:
					$this->type = "方法";
					break;
			}
			
			$this->display();
		}
		//添加节点处理
		public function addNodeHandle(){
			//p($_POST);die;
			if(M('node')->add($_POST)){
				$this->success('添加成功', U(GROUP_NAME.'/Rbac/node'));
			} else {
				$this->error('添加失败');
			}
		}
		//权限列表
		public function access(){
			$rid = I('rid', 0, 'intval');
			$field = array('id', 'name', 'title', 'pid');
			$node = M('node')->order('sort')->field($field)->select();
			
			//原有权限
			$access = M('access')->where(array('role_id' => $rid))->getField('node_id', true);
			$this->node = node_merge($node, $access);
			$this->rid = $rid;
			$this->display();
		}
        //权限处理
		public function setAccess(){
			$rid = I('rid', 0, 'intval');
			$db = M('access');
			
			//删除原来的权限
			$db->where(array('role_id' => $rid))->delete();
			
			$data = array();
			foreach ($_POST['access'] as $v){
				$tmp = explode('_', $v);
				$data[] = array(
					'role_id' => $rid,
					'node_id' => $tmp[0],
					'level' => $tmp[1],
				);
			}
			
			if($db->addAll($data)){
				$this->success('添加成功', 'role');
			} else {
				$this->error('添加失败');
			}
		}
        /*锁定管理员*/
        public function lock()
        {
            /*
             * eger ochuq bolsa quluplaydu*/
            if (I('lock')) {
                if (M('user')->where(array('id' => array('eq', $_GET['id'])))->save(array('lock' => 1))) {
                    $this->success('锁定成功', U('index'));
                } else {
                    $this->error('锁定失败');
                }
            } /*eger quluplanghan bolsa yeshidu*/
            else {
                if (M('user')->where(array('id' => array('eq', $_GET['id'])))->save(array('lock' => 0))) {
                    $this->success('解锁成功', U('index'));
                } else {
                    $this->error('解锁失败');
                }
            }

        }
        /*
          * 删除管理员*/
        public function delete()
{
    if (M('user')->where(array('id' => array('eq', $_GET['id'])))->delete()) {
        $this->success('删除成功', U('index'));
    } else {
        $this->error('删除失败');
    }
}
        /*
        * 删除角色*/
        public function deleterole()
{
    if (M('role')->where(array('id' => array('eq', $_GET['rid'])))->delete()) {
        $this->success('删除成功', U('role'));
    } else {
        $this->error('删除失败');
    }
}
        /*
          * 删除节点*/
        public function deletenode()
{
    if (M('node')->where(array('id' => array('eq', $_GET['id'])))->delete()) {
        $this->success('删除成功', U('node'));
    } else {
        $this->error('删除失败');
    }
}
        /*
         * 角色开关*/
        public function on_off_role()
{
    if (I('status')) {
        if (M('role')->where(array('id' => array('eq', $_GET['rid'])))->save(array('status' => 0))) {
            $this->success('关闭成功', U('role'));
        } else {
            $this->error('关闭失败');
        }
    } /*eger quluplanghan bolsa yeshidu*/
    else {
        if (M('role')->where(array('id' => array('eq', $_GET['rid'])))->save(array('status' => 1))) {
            $this->success('开启成功', U('role'));
        } else {
            $this->error('开启失败');
        }
    }
}
        /*
        * 修改管理员密码*/
        public function edit_user_pwd()
        {
            $this->admin_id = M('user')->where(array('id' => array('eq', $_GET['id'])))->select();
            $this->display();
        }
        /*
        * 修改管理员密码处理*/
        public function run_edit_user_pwd()
        {
            /* ikki parolning oxshashliqini tekshurmiz*/
            if (I('ADM_password_1') != I('ADM_password_2')) {
                $this->error('两次密码不配');
            }

            $data = array(
                'password' => I('ADM_password_2', '', 'md5'),
                'lock' => I('ADM_lock')
            );
            /*
             * tehrirlengen uchurni sandangha qoshimiz*/
            if (M('user')->where(array('id' => array('eq', $_POST['ADM_id'])))->save($data)) {
                $this->success('修改成功', U('index'));
               } else {
                $this->error('修改失败');
               }
        }
        /*发件箱设置*/
        public function email(){
            $this->display();
        }
        /*发件箱设置处理*/
        public function emailMSG(){
            if(F('email',$_POST,CONF_PATH)){
                $this->success('修改成功',U(GROUP_NAME.'/Rbac/email'));
            }else{
                $this->error('修改失败');
            }
        }
        /*网站信息*/
        public function sestem(){
            $this->display();
        }
        /*网站信息设置处理*/
        public function sestemMSG(){
            $data=array(
                'WEB_NAME'=>I('WEB_NAME'),
                'VERIFY_INFO'=>I('VERIFY_INFO'),
                'ICP_INFO'=>I('ICP_INFO'),
                'SCHOOL_PHONE'=>I('SCHOOL_PHONE'),
                'SCHOOL_ADDRESS'=>I('SCHOOL_ADDRESS'),
                'SCHOOL_EMAIL'=>I('SCHOOL_EMAIL')
            );
            if (!empty($_FILES['web_logo']['name'])) {
                //如果有文件上传 上传附件
                $this->uploadify();
                $data=array_merge(array('web_logo'=>$_POST['school_pic']),$data);
            }else{
                $data=array_merge(array('web_logo'=>C('web_logo')),$data);

            }

            if(F('sestem',$data,CONF_PATH)){
                $this->success('修改成功',U(GROUP_NAME.'/Rbac/sestem'));
            }else{
                $this->error('修改失败');
            }
        }
        //校长信息与书记信息
        public function school_masters(){
            $school_page=$_GET['select']?'school_master':'school_Secretary';
            $this->display($school_page);
        }
        //学校信息处理
        public function run_school_info(){

            $config_select=$_GET['config_select']?'school_master':'school_secretary';

            $data=array(
                $config_select.'_name'=>  I($config_select.'_name'),
                $config_select.'_email'=> I($config_select.'_email'),
                $config_select.'_addres'=>I($config_select.'_addres'),
                $config_select.'_phone'=> I($config_select.'_phone'),
            );

            if (!empty($_FILES[$config_select.'_pic']['name'])) {
                //如果有文件上传 上传附件
                $this->uploadify();
                $data=array_merge(array($config_select.'_pic'=>$_POST['school_pic']),$data);
            }else{
                $data=array_merge(array($config_select.'_pic'=>C($config_select.'_pic')),$data);

            }
            if(F($config_select,$data,CONF_PATH)){
                $this->success('修改成功',U(GROUP_NAME.'/Rbac/'.$config_select));
            }else{
                $this->error('修改失败');
            }
        }
        /*上传封面文件*/
        public function uploadify()
        {
            import("ORG.Util.UploadFile");
            $upload = new UploadFile();
            $upload->maxSize = 2048000;
            $upload->allowExts = array('jpg','jpeg','gif','png');
            $upload->savePath = './gulbagh/uploads/thomb/';
            if(!$upload->upload()){
                $this->error($upload->getErrorMsg());//获取失败信息
            } else {
                $info = $upload->getUploadFileInfo();//获取成功信息
                $_POST['school_pic'] =$info[0]['savename'];

            }
        }

        public function uploadify_miusic()
        {
            import("ORG.Util.UploadFile");
            $upload = new UploadFile();
            $upload->maxSize = 20048000;
            $upload->allowExts = array('mp3');
            $upload->savePath = './gulbagh/uploads/mp3/';
            if(!$upload->upload()){
                $this->error($upload->getErrorMsg());//获取失败信息
            } else {
                $info = $upload->getUploadFileInfo();//获取成功信息
                $_POST['WEB_MIUSIC'] =$upload->savePath.$info[0]['savename'];

            }
        }
}
?>